<?php
class Admin_ProductColorController extends Apps_Controller_Action_Admin {
    public function readAction() {

        $q = $this->_dbAdapter->select()->from('product_color')->where('status !=?', -1);

        $grid = new Grid_Product_Color('product-color-grid');
        $grid->setCaption('Color Product List');
        $grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($q));
        $grid->setView(new OeZend_Widget_Grid_View());

        $grid->deploy();

        $this->view->grid = $grid;
    }

    public function createAction() {
        $rq = $this->_request;

        $tbProductColor = new Model_DbTable_ProductColor();
        $f 	= new Admin_Form_AddProductColor();

        if ($rq->isPost()) {
            if ($f->isValid($rq->getParams())) {
                $this->_flashMessenger->addMessage(
                        $tbProductColor->insert($f->getValues())
                        ? self::$msgAddSuccess
                        : self::$msgAddFailed);

                $this->_helper->redirector('read', null, null);
            }
        }
        $f->setLegend('Add New Product Color');
        $this->view->form = $f;
    }

    public function updateAction() {
        $rq = $this->_request;
        $id = $rq->getParam('id');

        $tbProductColor = new Model_DbTable_ProductColor();

        if(!$id || !is_numeric($id) || !$tbProductColor->fetchRow(array('id =?' => $id))) {
            $this->_helper->redirector('read', null, null);
        }

        $f 	= new Admin_Form_AddProductColor();
        if ($rq->isPost()) {
            if ($f->isValid($rq->getParams())) {
                $this->_flashMessenger->addMessage(
                        $tbProductColor->update($f->getValues(), array('id =?' => $id))
                        ? self::$msgUpdateSuccess
                        : self::$msgUpdateFailed);
                $this->_helper->redirector('read', null, null);
            }
        }

        $f->setLegend('Update Product Color');
        $f->populate($tbProductColor->fetchRow(array('id =?' => $id))->toArray());
        $this->view->form = $f;
        $this->render('create');
    }

    //DELETE
    public function deleteAction() {
        $this->disableLayout(true, true);

        $id = $this->_getParam('id')
        ? $this->_getParam('id')
        : $this->_getParam('item_checker');

        if (!$id) {
            $this->_helper->redirector('read', null, null);
        }

        $tbProductColor = new Model_DbTable_ProductColor();
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (is_numeric($v)) {
                    $tbProductColor->update(array('status' => $tbProductColor::STATUS_DELETED),
                            array('id =?'  => $v));
                }
            }
        } else if (is_numeric($id)) {
            if ($tbProductColor->update(array('status' => $tbProductColor::STATUS_DELETED),
                    array('id =?'  => $id)))
            {
                $this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
            }
        }
        $this->_helper->redirector('read', null, null);
    }

}